﻿#ifndef CMAINFRAME_H
#define CMAINFRAME_H

#include "common.h"
#include "singleton.h"
#include "scripteditor.h"
#include "dropdownlistpanel.h"
#include "networkframemanager.h"
#include "wsignalserver.h"

class CMainFrame : public wxFrame ,
                   public Singleton<CMainFrame>,
                   public NetworkFrameManager
{
    DECLARE_EVENT_TABLE()

public:
    CMainFrame(const wxString& title,
               const wxPoint& pos = wxDefaultPosition,
               const wxSize& size = wxDefaultSize);
    ~CMainFrame();

    /// 打印日志
    void printlog(int level,const char* str,bool issave=true);

    /// 开始定时器
    void startTimer(wxString name,int interval,int runCount,luabridge::LuaRef pcallback);
    /// 停止指定定时器
    void stopTimer(wxString name);
    /// 停止所有定时器
    void stopAllTimer(void);
    /// 处理所有定时器事件
    void onProcessAllTimerEvent(void);

    /// 得到所有的信号
    wxString getSignals(wxVector<wxString> nodes=wxVector<wxString>());
    /// 更新所有的信息
    bool updateSignals(const char* clientid,const char* signalMsg,int msglength);
    /// 发送信息到服务器
    void sendSignals(int msgId,const char* signalMsg,int msglength,const char* clientid=NULL);
    /// 发送信息到服务器
    void sendSignals2(int msgId,wxString signalMsg,const char* clientid=NULL);

    /// 处理网络二进制消息
    virtual void OnProcessNetData(ClientInfo* client,int msgType,wxMemoryBuffer& data);
    /// 处理一个新的连接到达
    virtual void OnProcessConnectedNet(ClientInfo* client);

protected:
    void OnClose(wxCloseEvent& event);
    void OnMainFrameMenu(wxCommandEvent& event);
    void OnDrapListSelectedEvent(wxThreadEvent& event);
    void OnSystemInitTimer(wxTimerEvent& event);
    void OnSystemScriptTimer(wxTimerEvent& event);
    void OnKeyDown(wxKeyEvent& event);

private:
    wxTimer *m_sysInitTimer;
    wxTimer *m_scriptTimer;
    wxAuiManager m_auimgr;
    wxRichTextCtrl* m_systemlogtextCtrl;               /**< 系统日志输出 */
    MainScriptEditor* m_MainScriptEditor;              /**< 脚本编辑 */
    DropDownListPanel *m_pSwitchDropDownListPanel;
    DropDownListPanel *m_pSignalDropDownListPanel;
    DropDownListPanel *m_pSectDropDownListPanel;

    wxMutex m_TimerMutex;
    std::map<wxString,tagTimerItem> m_TimerItems;
};

#endif // CMAINFRAME_H
